<?php
class InvitationDao {
	
	private $mysqli;
	
	/*
	 * 连接数据库
	 * @author:cjy
	 */
	public function InvitationDao() {
		$this->mysqli = new mysqli('10.4.12.173', 'uZavFD5HSjZwz', 'p9KZTHKCUR8Kw','d2eed217eab864fbe960ee093bed9e1e2');
		mysqli_query($this->mysqli,'set names utf8');
	}
	
	/**
	 * 用户发单子
	 * @param unknown $postID   发单用户id
	 * @param unknown $content  需求内容
	 * @param unknown $address  地址
	 * @param unknown $cate     类别
	 * @param unknown $posttime  发帖时间
	 * @param unknown $starttime  开始时间
	 * @param unknown $endtime  结束时间
	 * @param unknown $money   金额
	 * @param unknown $long   用户发帖时经度
	 * @param unknown $lati   用户发帖时纬度
	 */
	public function post($postID,$content,$address,$cate,
			$posttime,$starttime,$endtime,$money,$long,$lati) {
		$this->mysqli->autocommit(false);
		$query = "INSERT INTO invitation(o_address,o_content,o_cate,o_money
				,o_long,o_lati,o_ptime,o_stime,o_etime,o_puid)
				 VALUES('".$address."','".$content."','".$cate."','".$money."','
				 ".$long."','".$lati."','".$posttime."','".$starttime."','".$endtime."','".$postID."');";
		mysql_query("SET NAMES utf-8");
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		if(!$this->mysqli->errno) {
			$this->mysqli->commit();
			return 1;
		} else {
			$this->mysqli->rollback();
			return 0;
		}
		
	}
	
	
	/**
	 * 按照发帖时间降序查询帖子 
	 * @param unknown $rows : 从0行开始 每次偏移rows行
	 */
	public function selectPostByPostTime($rows) {
		$query = "SELECT * FROM invitation,user WHERE invitation.o_puid = user.u_id ORDER BY o_ptime DESC LIMIT 0,".$rows.";";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		$row = array();$rows = array();$i = 0;
		while($temp = mysqli_fetch_array($result))
		{
			$row['o_id'] = $temp['o_id'];
			$row['o_address'] = urlencode($temp['o_address']);
			$row['o_cate'] = urlencode($temp['o_cate']);
			$row['o_content'] = urlencode($temp['o_content']);
			$row['o_lati'] = $temp['o_lati'];
			$row['o_long'] = $temp['o_long'];
			$row['o_money'] = $temp['o_money'];
			$row['o_staus'] = $temp['o_staus'];
			$row['o_stime'] = urlencode($temp['o_stime']);
			$row['o_etime'] = urlencode($temp['o_etime']);
			$row['o_ptime'] = urlencode($temp['o_ptime']);
			$row['o_username'] = urlencode($temp['u_username']);
			$rows[$i++] = $row;
		}
		return urldecode(json_encode($rows));
		
	}
	
	/**
	 * 按发帖的金额排序
	 * @param unknown $rows
	 */
	public function selectPostByMoney($rows) {
		$query = "SELECT * FROM invitation,user WHERE invitation.o_puid = user.u_id ORDER BY o_money DESC LIMIT 0,".$rows.";";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		$row = array();$rows = array();$i = 0;
		while($temp = mysqli_fetch_array($result))
		{
			$row['o_id'] = $temp['o_id'];
			$row['o_address'] = urlencode($temp['o_address']);
			$row['o_cate'] = urlencode($temp['o_cate']);
			$row['o_content'] = urlencode($temp['o_content']);
			$row['o_lati'] = $temp['o_lati'];
			$row['o_long'] = $temp['o_long'];
			$row['o_money'] = $temp['o_money'];
			$row['o_staus'] = $temp['o_staus'];
			$row['o_stime'] = urlencode($temp['o_stime']);
			$row['o_etime'] = urlencode($temp['o_etime']);
			$row['o_ptime'] = urlencode($temp['o_ptime']);
			$row['o_username'] = urlencode($temp['u_username']);
			$rows[$i++] = $row;
		}
		return urldecode(json_encode($rows));
	}
	
	
	/**
	 * 根据榜单的ID修改榜单的状态
	 * @param unknown $id
	 * @return number
	 */
	public function updatePostStatusById($id,$aid) {
		$showtime = date("Y年m月d日 H:i");
		$query = "UPDATE invitation SET o_staus = 1 , o_atime ='".$showtime."' , o_acid = '".$aid."' WHERE o_id = ".$id.";";
		$this->mysqli->autocommit(false);
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		if(!$this->mysqli->errno) {
			$this->mysqli->commit();
			return 1;
		} else {
			$this->mysqli->rollback();
			return 0;
		}
	}
	
	public function getUserEmailByPostId($id) {
		$query = "SELECT user.u_email,invitation.o_content,user.u_username FROM user,invitation
				WHERE user.u_id=invitation.o_puid AND invitation.o_id=".$id;
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		if($result->num_rows) {
			$temp = $result->fetch_row();
			return $temp;
		} else {
			return 0;
		}
	}
	
	/**
	 * 根据id查找用户所发榜单
	 * @param unknown $id
	 * @return string
	 */
	public function selectPostByPostId($id) {
		$query = "SELECT o_address,o_content,o_ptime,o_money,o_atime From invitation
				WHERE o_puid = ".$id;
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		$row = array();$rows = array();$i = 0;
		while($temp = mysqli_fetch_array($result))
		{
			$row['o_address'] = urlencode($temp['o_address']);
			$row['o_content'] = urlencode($temp['o_content']);
			$row['o_money'] = $temp['o_money'];
			$row['o_atime'] = urlencode($temp['o_atime']);
			$row['o_ptime'] = urlencode($temp['o_ptime']);
			$rows[$i++] = $row;
		}
		return urldecode(json_encode($rows));
	}
	
	/**
	 * 根据id查找用户所收榜单
	 * @param unknown $id
	 * @return string
	 */
	public function selectPostByAcceptId($id) {
		$query = "SELECT o_address,o_content,o_ptime,o_money,o_atime From invitation
				WHERE o_acid = ".$id;
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		$row = array();$rows = array();$i = 0;
		while($temp = mysqli_fetch_array($result))
		{
			$row['o_address'] = urlencode($temp['o_address']);
			$row['o_content'] = urlencode($temp['o_content']);
			$row['o_money'] = $temp['o_money'];
			$row['o_atime'] = urlencode($temp['o_atime']);
			$row['o_ptime'] = urlencode($temp['o_ptime']);
			$rows[$i++] = $row;
		}
		return urldecode(json_encode($rows));
	}
}